﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TLS05:UILIEFKO
    {

        public TLS05()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VLIEFKO();
            DataContext = MV;
            InitializeComponent();
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                KEY = "";
                LIEFBED = "";
                ZAHLBED = "";
                VSPED = "";
                SART = "";
            }
            else
            {
                KEY = (dwMaster["LSKLIEFNR"].ToString() + dwMaster["LSKKONDSL"].ToString()).PadRight(12);
                LIEFBED = dwMaster["LSKLIEFBED"].ToString();
                ZAHLBED = dwMaster["LSKZAHLBED"].ToString();
                VSPED = dwMaster["LSKSPEDSL"].ToString();
                SART = dwMaster["LSKVERSART"].ToString();
            }
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                dwData = TBMain.NewRow();
                ((VLIEFKO)MV).SetUpNewRow(ref dwData);
                LFD = VSDDAT.SD22.GETNEXT("0022", "LS", LIEFNR, 1);
                dwData["LSKLIEFNR"] = LIEFNR;
                dwData["LSKKONDSL"] = Helpers.Helper.Right(LFD, 4);
                ((VLIEFKO)MV).DeleteLIEFKO(dwData, 1);
                ((VLIEFKO)MV).InsertLIEFKO(dwData, app.company);
                TBMain.Rows.InsertAt(dwData, 0);
                vwMaster.View.MoveCurrentToFirst();
                base.NewCommand(sender, e);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            //if (dwMaster == null)
              //  return;
            //((VLIEFKO)MV).UpdateLIEFKO(dwMaster, app.company, 0);
            base.SaveCommand(sender, e);
        }
        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwMaster.ItemArray;
            LFD = VSDDAT.SD22.GETNEXT("0022", "LS", LIEFNR, 1);
            dwData["LSKKONDSL"] = LFD;
            ((VLIEFKO)MV).DeleteLIEFKO(dwData, 1);
            ((VLIEFKO)MV).InsertLIEFKO(dwData, app.company);
            TBMain.Rows.InsertAt(dwData, 0);
            vwMaster.View.MoveCurrentToFirst();
            base.CopyCommand(sender, e);
        }
        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            IsSaved = false;
            base.EditCommand(sender, e);
        }
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            ((VLIEFKO)MV).DeleteLIEFKO(dwMaster, 1);
            string filter = "LSKKONDSL='" + dwMaster["LSKKONDSL"].ToString() + "'";
            DataRow[] rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
        }
        public override void LOADBYAUF()
        {
            if (!IsSaved)
                return;
            if (ORDERNUM.Length < 12)
                return;
            if (ORDERNUM != KEY)
                return;
            dwData = TBMain.NewRow();
            dwData["LSKMANDANT"] = app.Mandant;
            dwData["LSKLIEFNR"] = " AND LSKLIEFNR='" + ORDERNUM.Substring(0, 8) + "'";
            dwData["LSKKONDSL"] = " AND LSKKONDSL='" + ORDERNUM.Substring(8, 4) + "'";
            SResult = ((VLIEFKO)MV).QueryLIEFKO(dwData, 1);
            DataTable tbTemp = TBMain.Clone();
            tbTemp.Merge(SResult.Tables[0]);
            foreach (DataRow row in tbTemp.Rows)
            {
                dwMaster.ItemArray = row.ItemArray;
            }
            base.LOADBYAUF();
        }
        public override void OnLIEFNRChg(string value)
        {
            base.OnLIEFNRChg(value);
            TBMain.Clear();
            if (value.Trim().Length < 8)
            { //00000000
                return;
            }
            dwData = TBMain.NewRow();
            dwData["LSKMANDANT"] = app.Mandant;
            dwData["LSKLIEFNR"] = " AND LSKLIEFNR='" + value.Substring(0, 8) + "'";
            SResult = ((VLIEFKO)MV).QueryLIEFKO(dwData, 1);
            MergeData(SResult, true); 
            if (!IsAuto)
                return;
            if (TBMain.Rows.Count > 0)
                return;
            if (value.Length == 0)
                return;
            dwData = TBMain.NewRow();
            ((VLIEFKO)MV).SetUpNewRow(ref dwData);
            dwData["LSKMANDANT"] = app.Mandant;
            dwData["LSKLIEFNR"] = value;
            dwData["LSKKONDSL"] = "STD".PadRight(4);
            ((VLIEFKO)MV).InsertLIEFKO(dwData, app.company);
            TBMain.Rows.InsertAt(dwData, 0);
            vwMaster.View.MoveCurrentToFirst();
        }
       
    }
}
